<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <title>项目管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../assets/layui/css/layui.css" media="all">
</head>
<body>
<div style="padding: 10px;">
    <from class="layui-form" action="">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label" style="width: auto;">项目名称</label>
                <div class="layui-input-inline" style="width: 130px;">
                    <input type="text" id="projectName" name="projectName" autocomplete="off" class="layui-input">
                </div>
            </div>

            <div class="layui-inline">
                <label class="layui-form-label" style="width: auto;">施工单位名称</label>
                <div class="layui-input-inline" style="width: 130px;">
                    <input type="text" id="companyName" name="companyName" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label" style="width: auto;">工程状态</label>
                <div class="layui-input-inline" style="width: 90px;">
                    <select name="projectstatus"  id="projectstatus"  >
                        <option value="" selected="selected">请选择</option>
                        <option value="在建">在建</option>
                        <option value="竣工">竣工</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <button class="layui-btn layui-btn-normal" data-type="search" id="btnSearch">查询</button>
                <button class="layui-btn layui-btn-primary" data-type="doAdd" id="btnAdd">新增项目</button>
            </div>
        </div>
    </from>
</div>
<table class="layui-table" lay-filter="tbFliter"  style="margin-top: -25px;"  id="tbData">
</table>
<script type="text/html" id="barList">
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script src="../assets/layui/layui.js"></script>
<script>
    layui.config({
        base: '../js/'
    });

    layui.use(['jquery','table','common','laydate'], function () {

        var table = layui.table,
            common=layui.common,
            laydate=layui.laydate,
            $ = layui.jquery,
            active = {
                search: function () {
                    table.reload("tbOptions", {
                        where: {
                            projectName: $("#projectName").val(),
                            companyName: $("#companyName").val(),
                            projectstatus: $("#projectstatus").find("option:selected").val()
                        },page:{
                            curr: 1
                        }
                    });
                },
                doAdd: function () {
                    common.layer_show("项目新增",'projectAdd.html?type=add&op='+op,'100%','100%');
                },
                doImport:function () {

                },
                doExport:function () {

                }
            };
        var op=common.getUrlParam("op");
        var userType=common.getUrlParam("userType");
        init();
        function init(){
            if(userType=='2'){
                $("#btnAdd").hide();
            }
        }

        //表格初始化
        table.render({
            elem: '#tbData'
            // , skin: 'row'   //行边框风格
            // ,even: true //开启隔行背景
            , url: '../project/list'
            , page: true
            , cols: [
                [
                    {title: '项目信息',colspan:5,align:'center'}
                    ,{title: '施工单位信息',colspan:3,align:'center'}
                    , {
                    field: 'operation', title: '操作',align:'left',rowspan:2,
                    templet: function (e) {
                        var html = '';
                        if(userType=='admin'){
                            html += ' <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">编辑</a>';
                            if ('0' == e.status) {
                                html += '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="qiyong">启用</a>';
                            } else {
                                html += '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>';
                            }
                            // html += ' <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="allot">分配</a>';
                        }
                        else {
                            //一般账号
                            if (e.userType == '0') {
                                html += ' <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">编辑</a>';
                                if ('0' == e.status) {
                                    html += '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="qiyong">启用</a>';
                                } else {
                                    html += '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>';
                                }
                            }
                            //施工单位账号
                            else if (e.userType == '2'&&e.status=='2') {
                                // html += ' <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="allot">查看</a>';
                                html += ' <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="allot">项目完善</a>';
                                html += ' <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="allot">分配</a>';
                            }
                        }
                        return html;
                    }
                }],
                [
                 {field: 'projectname', title: '项目名称', templet: function (e) {
                        return  ' <a lay-event="detail" style="color: #fff;cursor: pointer" title="查看详情">'+e.projectname+'</a>';
                    }}
                ,{field: 'projectperson', title: '项目负责人',width: 100}
                ,{field: 'projecttype', title: '项目类型',width: 90}
                ,{field: 'projectadress', title: '项目地址',width: 90}
                ,{field: 'projectstatus', title: '项目状态',width: 90}
                ,{field: 'companyname',  title: '施工单位',width: 90}
                ,{field: 'company_user',  title: '施工单位负责人',width: 100}
                ,{field: 'company_user_phone', title: '施工单位负责人手机',width: 120}
            ]]
            , limits: [10, 20, 50]
            , where: {
                projectName: $("#projectName").val(),
                companyName: $("#companyName").val(),
                projectstatus: $("#projectstatus").find("option:selected").val(),
                op: op
            }
            , id: 'tbOptions'
        });

        table.on('tool(tbFliter)', function (obj) {
            var data = obj.data; //获得当前行数据
            var projectId = data.projectid;
            var layEvent = obj.event; //获得 lay-event
            if (layEvent === 'edit') { //编辑
                common.layer_show("项目信息编辑", 'projectEdit.html?type=edit&id=' + projectId+'&op='+op, '100%', '100%');
            }
            else if(layEvent === 'allot'){
                common.layer_show("项目分配", 'projectAllot.html?type=allot&id=' + projectId+'&op='+op, '100%', '100%');
            }
            else if(layEvent === 'detail'){
                common.layer_show("项目详情", 'projectDetail.html?type=allot&id=' + projectId+'&op='+op, '100%', '100%');
            }
            else if (layEvent === 'del'||layEvent === 'qiyong') { //删除
                 if(op!="1"){
                     common.showError("仅能由管理员删除项目");
                     return false;
                 }
                 var str="删除";
                 if (layEvent === 'qiyong'){
                     str='启用';
                 }
                layer.confirm('您确定'+str+'该项目吗', function (index) {
                    $.ajax({
                        type: 'POST',
                        url: '../project/delete',
                        dataType: 'json',
                        data: {projectId: projectId,status:layEvent},
                        success: function (rtn) {
                            if (rtn.code == 0) {
                                common.showMsg('操作成功!');
                                table.reload("tbOptions", {
                                    where: {
                                        projectName: $("#projectName").val(),
                                        companyName: $("#companyName").val(),
                                        projectstatus: $("#projectstatus").find("option:selected").val()
                                    },page:{
                                        curr: 1
                                    }
                                });
                                // obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                            }
                            else {
                                common.showError("操作失败:" + rtn.msg);
                            }
                        },
                        error: function () {
                            common.showError("操作失败");
                        }
                    });
                });

            }
        });

        //按钮事件绑定
        $('.layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>
</body>
</html>